var YAHOO_HISTORY_SOURCE="http://ichart.finance.yahoo.com/table.csv";
var YAHOO_QUOTE_NAME_SOURCE = "http://download.finance.yahoo.com/d/quotes.csv";
var YAHOO_URL = "http://finance.yahoo.com/q";


function loadQuoteNameYAHOO(_quotes,_loadFinished)
{
	var urlStr = getQuoteNameUrlYAHOO(_quotes);

	_IG_FetchContent(urlStr,function(response)
	{
		var nomes= response.split('\n');

		for( var i=0;i<_quotes.length;i++)
		{
			var data = nomes[i].split('"');
			_quotes[i].alias = data[1];
			_quotes[i].stockExchange = data[3];
		}
		_loadFinished();
	});

}

function loadQuoteCurrentValueYAHOO(_quotes,_loadFinished)
{
	var urlStr = getQuoteCurrentValueUrlYAHOO( _quotes);

	_IG_FetchContent(urlStr,function(response)
	{
		var values= response.split('\n');
		var today = new Date();
		today.setHours(0,0,0,0);
		console.debug("loaded the current value");

		for( var i=0;i<_quotes.length;i++)
		{
			var retrievedDate;
			var retrievedValues =values[i].split('"');

			var value = retrievedValues[1];
			if(value!=null)
			{
				var ini = value.lastIndexOf("<b>");
				ini +=3;
				var fim = value.lastIndexOf("</b>");
				value = value.substring(ini,fim);

				retrievedDate = getDate(retrievedValues[3],REAL_TIME_DATE);
				//if(today.getTime()==retrievedDate.getTime())
				{
					_quotes[i].currentCotation = new Cotation(retrievedDate,value);
				}
				//else
				//{
				//	_quotes[i].cotations.unshift(new Cotation(retrievedDate,value));
				//}
			}

		}
		_loadFinished(_quotes[0]);
	});

}



function loadQuoteDataYAHOO(_quote, _dateIni, _dateFinal,_receiver,_loadFinished)
{

	var urlData = getQuoteHistoryDataUrlYAHOO(_dateIni, _dateFinal, _quote.symbol);
	_IG_FetchContent(urlData,function(response)
	{
		var cots = new Array();
		var retrievedHistory = response.split("\n");
		var historyData;
		var fieldDateValue = 0;
		var fieldClosedValue = 4;

		for( var i=1;i<retrievedHistory.length-1;i++)
		{
			historyData = retrievedHistory[i].split(",");
			if(historyData.length !=7)  // 7 --> number of fields returned by yahoo
			{
				console.warn("Error loading quote information:\n\tQuote: %s\n\tDate Ini: %s\n\tDate Fim: %s\n\tUrl: %s\n\tRetrieved information: %s",_quote.symbol,getDateAsString(_dateIni),getDateAsString(_dateFinal),urlData,historyData);
				return;
			}
			cots.push(new Cotation(getDate(historyData[fieldDateValue],YAHOO_DATE),historyData[fieldClosedValue]));
		}
		if(_receiver.constructor==InvestedValue)
		{
			_receiver.addQuoteData(_quote,cots[0]);
		}
		else if (_receiver.constructor==Cotation)
		{
			_receiver.value = cots[0].value;
		}
		else if(_receiver.constructor==Quote)
		{
			for( var i=0;i<cots.length;i++)
			{
				if(_receiver.getCotation(cots[i].date)== null)
				{
					_receiver.cotations.push(cots[i]);
				}
			}
		}
		_loadFinished(_receiver);
	})
}

function getQuoteNameUrlYAHOO(_quotes)
{
  var baseUrl = YAHOO_QUOTE_NAME_SOURCE;
  var quotesSymbols = '';

  if(_quotes.length > 0)
  	quotesSymbols = _quotes[0].symbol;

   for( var i=1;i<_quotes.length;i++)
   {
     quotesSymbols +='+'+_quotes[i].symbol
   }

  return baseUrl +"?s="+quotesSymbols+"&f=nx";
}

function getQuoteCurrentValueUrlYAHOO(_quotes)
{
  var baseUrl = YAHOO_QUOTE_NAME_SOURCE;
  var quotesSymbols = '';
  var data = new Date();
  data.setHours(data.getHours(),data.getMinutes(),0,0);
  var time = data.getTime();

  if(_quotes.length > 0)
  	quotesSymbols = _quotes[0].symbol;

   for( var i=1;i<_quotes.length;i++)
   {
     quotesSymbols +='+'+_quotes[i].symbol
   }

  return baseUrl +"?s="+quotesSymbols+"&f=k1d1&time="+time;
}

 function getQuoteHistoryDataUrlYAHOO(_dateIni, _dateFinal, _symbol)
{
	var baseUrl = YAHOO_HISTORY_SOURCE;
	var data = new Date();
  	data.setHours(data.getHours(),0,0,0);
  	var time = data.getTime();

	var urlFinal = baseUrl +"?a="+_dateIni.getMonth()+"&b="+_dateIni.getDate()+"&c="+_dateIni.getFullYear();
	urlFinal = urlFinal + "&d="+_dateFinal.getMonth()+"&e="+_dateFinal.getDate()+"&f="+_dateFinal.getFullYear();
	urlFinal = urlFinal + "&g=d&s="+_symbol+"&time="+time;
	return urlFinal;
}

function getQuoteUrlYAHOO(_quoteSymbol)
{
	var res = YAHOO_URL+'?s='+_quoteSymbol;

	return res;
}